#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Models/CalibratedModel.h>
#include <gen/QL/Models/AffineModel.h>
#pragma unmanaged 
#include <ql\legacy\libormarketmodels\liborforwardmodel.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL::Math;
using namespace Cephei::QL::Termstructures::Volatility::Swaption;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Legacy { namespace Libormarketmodels {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ILiborForwardModel
	public ref class CLiborForwardModel : 
            public CCalibratedModel,
            public Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModel
	{
	protected: 
		boost::shared_ptr<QuantLib::LiborForwardModel>* _ppLiborForwardModel;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::LiborForwardModel>* _phLiborForwardModel;
#endif
		Object^ _LiborForwardModelOwner;     // reference to object that manages the storage for this object
	internal:
		CLiborForwardModel (Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModelProcess^ process, Cephei::QL::Legacy::Libormarketmodels::ILmVolatilityModel^ volaModel, Cephei::QL::Legacy::Libormarketmodels::ILmCorrelationModel^ corrModel);
        CLiborForwardModel (boost::shared_ptr<QuantLib::LiborForwardModel>& childNative, Object^ owner);
        CLiborForwardModel (QuantLib::LiborForwardModel& childNative, Object^ owner);
        CLiborForwardModel (CLiborForwardModel^ copy);
        CLiborForwardModel (System::Type^ t);
#ifdef STRUCT
        CLiborForwardModel (QuantLib::LiborForwardModel childNative);
#endif       
#ifdef HANDLE
		CLiborForwardModel (QuantLib::Handle<QuantLib::LiborForwardModel>& childNative, Object^ owner);
		CLiborForwardModel (QuantLib::Handle<QuantLib::LiborForwardModel> childNative);
#endif
		virtual ~CLiborForwardModel ();
		!CLiborForwardModel ();

	internal:
		QuantLib::LiborForwardModel& GetReference ();
		boost::shared_ptr<QuantLib::LiborForwardModel>& GetShared ();
		QuantLib::LiborForwardModel* GetPointer ();
        void SetLiborForwardModel (boost::shared_ptr<QuantLib::LiborForwardModel> native)
        {
            if (_ppLiborForwardModel != NULL)
                delete _ppLiborForwardModel;
            _ppLiborForwardModel = new boost::shared_ptr<QuantLib::LiborForwardModel> (native);
            SetCalibratedModel (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppLiborForwardModel));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::LiborForwardModel>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Double Discount (Double t) ;
		virtual Double DiscountBond (Double now, Double maturity, Cephei::QL::Math::IArray^ factors) ;
		virtual Double DiscountBondOption (QL::Option::TypeEnum type, Double strike, Double maturity, Double bondMaturity) ;
        property Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityMatrix^ GetSwaptionVolatilityMatrix 
        {
		    virtual Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityMatrix^ get () ;
        }
		virtual Double S_0 (UInt64 alpha, UInt64 beta) ;
		virtual Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModel^ SetParams (Cephei::QL::Math::IArray^ p_params) ;
    private:
        Cephei::QL::Models::CAffineModel^ _AffineModel;
    private:
        property CAffineModel^ AffineModel
        {
            CAffineModel^ get ()
            {
                if (_AffineModel == nullptr)
                {
                    _AffineModel = gcnew CAffineModel (boost::dynamic_pointer_cast<QuantLib::AffineModel> (*_ppLiborForwardModel), this);
                }
                return _AffineModel;
            }
        }
    public:
        operator Cephei::QL::Models::IAffineModel^ ()
        {
            return static_cast<Cephei::QL::Models::IAffineModel^>(AffineModel);
        }
        operator Cephei::QL::Models::CAffineModel^ ()
        {
            return AffineModel;
        }
        
    public:
        virtual Double M743_Discount (Double t)
        {
            return ((Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModel^)this)->Discount ( t);
        }
        virtual Double M743_DiscountBond (Double now, Double maturity, Cephei::QL::Math::IArray^ factors)
        {
            return ((Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModel^)this)->DiscountBond ( now,  maturity,  factors);
        }
        virtual Double M743_DiscountBondOption (QL::Option::TypeEnum type, Double strike, Double maturity, Double bondMaturity)
        {
            return ((Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModel^)this)->DiscountBondOption ( type,  strike,  maturity,  bondMaturity);
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CLiborForwardModel_Factory : public System::MarshalByRefObject,  public ILiborForwardModel_Factory
	{
	public:
        virtual ILiborForwardModel^ Create (Cephei::QL::Legacy::Libormarketmodels::ILiborForwardModelProcess^ process, Cephei::QL::Legacy::Libormarketmodels::ILmVolatilityModel^ volaModel, Cephei::QL::Legacy::Libormarketmodels::ILmCorrelationModel^ corrModel);
    };
   
/*Cephei*/ } /*QL*/ } /*Legacy*/ } /*Libormarketmodels */}
